//
//  LGSchedulHomeHeaderView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/28.
//

#import "LGSchedulHeaderView.h"

@interface LGSchedulHeaderView()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UISwitch *switchBtn;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIButton *touchBtn;
/**   */
@property (nonatomic , copy)NSString *openString;

@end

@implementation LGSchedulHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.switchBtn];
        [self.baseView addSubview:self.contentLabel];
        [self.baseView addSubview:self.lineView];
//        [self.baseView addSubview:self.touchBtn];
    }
    return self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.openString = [NSString stringWithFormat:@"已开启，患者可以向您预约%@",dataDic[@"title"]];
    self.isOpen = ([dataDic[@"is_closed"] integerValue] == 0)?YES:NO;
}


-(void)setIsOpen:(BOOL)isOpen{
    _isOpen = isOpen;
    self.switchBtn.on = isOpen;
    self.lineView.hidden = !isOpen;
    self.contentLabel.text = (isOpen == YES)?self.openString:@"已关闭，暂不接诊";
}


-(void)switchBtnAction:(UISwitch *)sender{
    if(sender.on == NO){
        if([LGNSString(self.dataDic[@"clinic_type"]) isEqualToString:@"3"]){
            //专家咨询不弹框
            if(self.delegate && [self.delegate respondsToSelector:@selector(openOrCloseServiceType:isClosed:)]){
                [self.delegate openOrCloseServiceType:LGNSString(self.dataDic[@"clinic_type"]) isClosed:@"1"];
            }
        }else{
            sender.on = YES;
            __weak typeof(self) weakSelf = self;
            [LGAlertTool alertWithTitle:@"关闭接诊" content:@"关闭接诊后，患者将无法再预约，但已预约患者仍保持原先预约状态。" sureTitle:@"确定关闭" sureAction:^{
                if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(openOrCloseServiceType:isClosed:)]){
                    [weakSelf.delegate openOrCloseServiceType:LGNSString(weakSelf.dataDic[@"clinic_type"]) isClosed:@"1"];
                }
            }];
        }
        
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(openOrCloseServiceType:isClosed:)]){
            [self.delegate openOrCloseServiceType:LGNSString(self.dataDic[@"clinic_type"]) isClosed:@"0"];
        }
    }
}


-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(5), Screen_W-viewPix(32), viewPix(90))];
        [_baseView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(17), viewPix(100), viewPix(20));
    }
    return _titleLabel;
}

-(UISwitch *)switchBtn{
    if(!_switchBtn){
        _switchBtn = [[UISwitch alloc]initWithFrame:CGRectMake(Screen_W-viewPix(32)-viewPix(66), viewPix(12), viewPix(50), viewPix(30))];
        _switchBtn.onTintColor = [UIColor colorWithString:@"#429BFF"];
        _switchBtn.tintColor = [UIColor colorWithString:@"#E9E9E9"];
        [_switchBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _contentLabel.frame = CGRectMake(viewPix(12), viewPix(45), Screen_W-viewPix(56), viewPix(20));
    }
    return _contentLabel;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(80)-1, Screen_W-viewPix(56), 0.5)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E8E8E8"];
        _lineView.hidden = YES;
    }
    return _lineView;
}

-(UIButton *)touchBtn{
    if(!_touchBtn){
        _touchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _touchBtn.frame = CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(80));
//        [_touchBtn addTarget:self action:@selector(touchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _touchBtn;
}

@end
